package com.dyc.controller;

import com.dyc.pojo.Emp;
import com.dyc.pojo.EmpQueryParam;
import com.dyc.pojo.PageResult;
import com.dyc.pojo.Result;
import com.dyc.service.EmpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/emps")
@RestController
public class EmpController {
    private static final Logger log = LoggerFactory.getLogger(DeptController.class);

    @Autowired
    private EmpService empService;

    /**
     * 分页查询
     *
     * @param empQueryParam 实体类
     * @return
     */
    @GetMapping
    //通过封装类EmpQueryParam优化
    public Result page(EmpQueryParam empQueryParam) {
        log.info("分页查询:{}", empQueryParam);
        PageResult<Emp> pageResult = empService.page(empQueryParam);
        return Result.success(pageResult);
    }

    /**
     * 新增员工
     *
     * @param emp
     * @return
     */
    @PostMapping
    public Result addEmp(@RequestBody Emp emp) {
        log.info("新增员工:{}", emp);
        empService.add(emp);
        return Result.success();
    }

    /**
     * 删除员工
     *
     * @return
     */
    //数组
   /* @DeleteMapping
    public Result delete(Integer[] ids) {
        log.info("删除员工:{}", Arrays.toString(ids));
        return Result.success();
    }*/
    //集合
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids) {
        log.info("删除员工:{}", ids);
        empService.delete(ids);
        return Result.success();
    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("根据id查询员工信息{}",id);
        Emp emp = empService.getInfo(id);
        return Result.success(emp);
    }

    /**
     *
     * @param emp
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Emp emp) {
        log.info("修改员工{}",emp);
        empService.update(emp);
        return Result.success();
    }

    /**
     * 修改密码
     * @param emp
     * @return
     */
    @PutMapping("/password")
    public Result changePassword(@RequestBody Emp emp){
        String password = emp.getPassword();
        Integer id = emp.getId();
        log.info("修改id为:{}，新密码为：{}",id,password);
        empService.changePassword(id,password);
        return Result.success();
    }

}
